package poioperator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import poitools.MainFrame;

public class POIOperator {
	
	MainFrame mf = null;
	public static void main(String[] args) {
		
	}
	
	public POIOperator(MainFrame mf){
		this.mf = mf;
	}
	
	/**@param filepath1 第一个文件全路径
	 * @param filepath2 第二个文件全路径
	 * */
	public void readFilePath(String filepath1,String filepath2){
		if("".equals(filepath1)){
			mf.refreshMessage(mf.message1, "没有选择文件！");
			return;
    	}
    	if("".equals(filepath2)){
    		mf.refreshMessage(mf.message2, "没有选择文件！");
			return;
    	}
    	
		if(!(filepath1.endsWith(".xls") || filepath1.endsWith(".xlsx"))){
			mf.refreshMessage(mf.message1, "所选文件格式不正确！");
			return;
		}else if(!(filepath2.endsWith(".xls") || filepath2.endsWith(".xlsx"))){
			mf.refreshMessage(mf.message2, "所选文件格式不正确！");
			return;
		}
		
		try {
			if(filepath1.endsWith(".xls")){
				readFile(new HSSFWorkbook(getExcelStream(1, filepath1)));
			}
			if(filepath1.endsWith(".xlsx")){
				readFile(new XSSFWorkbook(getExcelStream(1, filepath1)));
			}
		} catch (IOException e) {
			mf.refreshMessage(mf.message1, e.getMessage());
			for(StackTraceElement st:e.getStackTrace()){
				mf.refreshMessage(mf.message1, st.toString());
			}
		}
		
		try {
			if(filepath2.endsWith(".xls")){
				readFile(new HSSFWorkbook(getExcelStream(2, filepath2)));
			}
			if(filepath2.endsWith(".xlsx")){
				readFile(new XSSFWorkbook(getExcelStream(2, filepath2)));
			}
		} catch (IOException e) {
			mf.refreshMessage(mf.message2, e.getMessage());
			for(StackTraceElement st:e.getStackTrace()){
				mf.refreshMessage(mf.message2, st.toString());
			}
		}
	}
	
	/**读文件
	 * */
	public void readFile(Workbook wb){
		int sheetCount = wb.getNumberOfSheets();
		List<Map<String,List<Map<String,String>>>> listMapSheet = new ArrayList<Map<String,List<Map<String,String>>>>();
		
		/**遍历sheet*/
		for(int i = 0;i<sheetCount;i++){
			Map<String, List<Map<String, String>>> mapSheet = new HashMap<String,List<Map<String,String>>>();
			Sheet sheet = wb.getSheetAt(i);
			int lastRowNum = sheet.getLastRowNum();
			/**遍历row*/
			for(int j = 0;j < lastRowNum ; j++){
				List<Map<String, String>> listRow = new ArrayList<Map<String, String>>();
				
				Row row = sheet.getRow(j);
				if(row != null){
					int lastCellNum = row.getLastCellNum();
					/**遍历cell*/
					for(int k = 0;k < lastCellNum; k++){
						
						Cell cell = row.getCell(k);
						if(cell != null){
							String str = cell.getStringCellValue();
							cell.getCellFormula();
							System.out.println(str);
						}
					}
				}
			}
		}
		
	}
	
	
	/**获取文件输入流*/
	public InputStream getExcelStream(int index,String filepath){
		
		InputStream is = null;
		try {
			File file = new File(filepath);
			 is = new FileInputStream(file);
			
		} catch (FileNotFoundException e) {
			
			if(index==1){
				mf.refreshMessage(mf.message1, e.getMessage());
				for(StackTraceElement st:e.getStackTrace()){
					mf.refreshMessage(mf.message1, st.toString());
				}
			}if(index ==2){
				mf.refreshMessage(mf.message2, e.getMessage());
				for(StackTraceElement st:e.getStackTrace()){
					mf.refreshMessage(mf.message2, st.toString());
				}
			}
		}
		return is;
	}
	
	
}
